/*
*  Copyright 2019-2023 CY
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.zhengjie.modules.xiao.rest;

import me.zhengjie.annotation.AnonymousAccess;
import me.zhengjie.annotation.Log;
import me.zhengjie.annotation.rest.AnonymousGetMapping;
import me.zhengjie.modules.xiao.domain.Teacher;
import me.zhengjie.modules.xiao.service.TeacherService;
import me.zhengjie.modules.xiao.domain.vo.TeacherQueryCriteria;
import lombok.RequiredArgsConstructor;
import java.util.List;

import me.zhengjie.utils.Result;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.*;
import java.io.IOException;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import me.zhengjie.utils.PageResult;

/**
* @author cg
* @date 2025-05-20
**/
@RestController
@RequiredArgsConstructor
@Api(tags = "设计师团队管理")
@RequestMapping("/api/teacher")
public class TeacherController {

    private final TeacherService teacherService;

    @Log("导出数据")
    @ApiOperation("导出数据")
    @GetMapping(value = "/download")
    @PreAuthorize("@el.check('teacher:list')")
    public void exportTeacher(HttpServletResponse response, TeacherQueryCriteria criteria) throws IOException {
        teacherService.download(teacherService.queryAll(criteria), response);
    }

    @GetMapping
    @Log("查询设计师团队")
    @ApiOperation("查询设计师团队")
    @AnonymousAccess
    public ResponseEntity<PageResult<Teacher>> queryTeacher(TeacherQueryCriteria criteria, Page<Object> page){
        if(criteria.getOrderType() == null){
            criteria.setOrderType("0");
        }
        return new ResponseEntity<>(teacherService.queryAll(criteria,page),HttpStatus.OK);
    }

    @GetMapping("getTeacherList")
    @Log("查询设计师团队")
    @ApiOperation("查询设计师团队")
    public ResponseEntity<List<Teacher>> getTeacherList(String name){
        return new ResponseEntity<>(teacherService.getTeacherList(name),HttpStatus.OK);
    }

    @PostMapping
    @Log("新增设计师团队")
    @ApiOperation("新增设计师团队")
    @PreAuthorize("@el.check('teacher:add')")
    public ResponseEntity<Object> createTeacher(@Validated @RequestBody Teacher resources){
        teacherService.create(resources);
        return new ResponseEntity<>(HttpStatus.CREATED);
    }

    @PutMapping
    @Log("修改设计师团队")
    @ApiOperation("修改设计师团队")
    @PreAuthorize("@el.check('teacher:edit')")
    public ResponseEntity<Object> updateTeacher(@Validated @RequestBody Teacher resources){
        teacherService.update(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @DeleteMapping
    @Log("删除设计师团队")
    @ApiOperation("删除设计师团队")
    @PreAuthorize("@el.check('teacher:del')")
    public ResponseEntity<Object> deleteTeacher(@RequestBody List<Integer> ids) {
        teacherService.deleteAll(ids);
        return new ResponseEntity<>(HttpStatus.OK);
    }

    @AnonymousGetMapping(value = "/list")
    public Result<List<Teacher>> list(TeacherQueryCriteria criteria){
        return Result.success(teacherService.queryAll(criteria));
    }

    @AnonymousGetMapping(value = "/findTeacherByDistrict")
    public Result<Object> findTeacherByDistrict(String district){
        Map<String, Object> res = teacherService.findTeacherByDistrict(district);
        if(res != null){
            return Result.success(res);
        }else{
            return Result.fail("500", "查询失败");
        }
    }

    @GetMapping("getTeacherById")
    @Log("查询设计师团队详情")
    @ApiOperation("查询设计团队详细信息")
    @AnonymousAccess
    public ResponseEntity<Teacher> getTeacherById(int id){
        return new ResponseEntity<>(teacherService.getTeacherById(id),HttpStatus.OK);
    }
}